﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CommonDevelop
{
    /// <summary>
    /// 验证码
    /// </summary>
    public sealed class ValidateHelper
    {
        /// <summary>
        /// 创建验证码--简化
        /// </summary>
        public static byte[] VerifyImage(ref string code)
        {
            int l = 4;  //验证码字符数
            int h = 22; //验证码图片高度
            int w = 16; //验证码图片宽度
            string t = "char"; //验证码类型：char,字符；num,数字

            VerifyCode VC = new VerifyCode();
            string str = VC.getRandomValidate(l, t);
            code = str;
            return VC.getImageValidate(str, h, w);
        }

        /// <summary>
        /// 随机码
        /// </summary>
        internal class VerifyCode
        {
            Random ran = new Random();

            /// <summary>
            /// 得到随机字符串
            /// </summary>
            internal string getRandomValidate(int len, string t)
            {
                int num;
                int tem;
                string rtuStr = "";
                for (int i = 0; i < len; i++)
                {
                    num = ran.Next();
                    if (t == "num")
                        tem = num % 10 + '0';//生成数字
                    else
                        tem = num % 26 + 'A';//生成字符

                    rtuStr += Convert.ToChar(tem).ToString();
                }
                return rtuStr;
            }
            /// <summary>
            /// 生成图像
            /// </summary>
            internal byte[] getImageValidate(string strValue, int h, int w)
            {
                int width = Convert.ToInt32(strValue.Length * w);
                Bitmap img = new Bitmap(width, h);
                Graphics gfc = Graphics.FromImage(img);
                gfc.Clear(Color.White);
                drawLine(gfc, img);
                Font font = new Font("arial", h / 2, FontStyle.Bold);
                System.Drawing.Drawing2D.LinearGradientBrush brush =
                    new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 1.5f, true);
                gfc.DrawString(strValue, font, brush, 3, 2);
                drawPoint(img);
                gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);

                using (MemoryStream stream = new MemoryStream())
                {
                    img.Save(stream, ImageFormat.Gif);
                    byte[] data = new byte[stream.Length];
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.Read(data, 0, Convert.ToInt32(stream.Length));
                    img.Dispose();
                    return data;
                }
            }
            private void drawLine(Graphics gfc, Bitmap img)
            {
                for (int i = 0; i < 10; i++)
                {
                    int x1 = ran.Next(img.Width);
                    int y1 = ran.Next(img.Height);
                    int x2 = ran.Next(img.Width);
                    int y2 = ran.Next(img.Height);
                    gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
            }
            private void drawPoint(Bitmap img)
            {
                int col = ran.Next();
                for (int i = 0; i < 100; i++)
                {
                    int x = ran.Next(img.Width);
                    int y = ran.Next(img.Height);
                    img.SetPixel(x, y, Color.FromArgb(col));
                }
            }
        }
    }
}
